import WxValidate from '../../utils/WxValidate.js'

const app = getApp()

let Validate = ""

Page({

  /**
   * 页面的初始数据
   */
  data: {
    code: "获取验证码",
    _timer: 60,
    mobile: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    clearInterval(this.timeoutId);

    const rules = {
      mobile: {
        required: true,
        tel: true
      },
      code: {
        required: true
      },
      new_password: {
        required: true
      },
      confirm_new_password: {
        required: true ,
        equalTo: "new_password"
      }
    }

    const messages = {
      mobile: {
        required: '请输入手机号码',
        tel: '请输入正确的手机号码'
      },
      code: {
        required: '请输入验证码',
      },
      new_password: {
        required: '请输入密码',
      },
      confirm_new_password: {
        required: '请输入密码' ,
        equalTo: "两次密码不一致"
      }
    }
    Validate = new WxValidate(rules, messages)

  },
  handleMobile(e){
    this.setData({ mobile: e.detail.value })
  },
  codeText(_this){
    const { code, _timer, mobile } = this.data;
    let timer = _timer
    
    this.timeoutId = setInterval(() => {
      timer--
      
      if (timer > 0) {
        
        _this.setData({ code: timer + '秒后重发', _timer: timer })
        
      } else if (timer <= 0) {
        _this.setData({ code: '获取验证码', _timer: 60 })
        clearInterval(_this.timeoutId)
      }
    }, 1000)
  },
  handleCode(e) {
    let _this = this;
    
    let phoneReg = /^1[34578]\d{9}$/g;
    const { code, _timer, mobile } = this.data;
    let timer = _timer
    if (code === '获取验证码' && !!phoneReg.test(mobile)) {
      if (code === '获取验证码' && timer === 60 ) {
        app.query('/api/valid_codes', { mobile: mobile ,type: 2 }, 'post').then(res => {
          
          if (res.msg === '发送成功' ||true) {
            app.showToast('发送成功' , 'success' ,500);
            _this.setData({ code: '60秒后重发' })
            _this.codeText(_this);
          }else{
            app.showToast(res.msg, false, 500, '../../assets/warning.png')
          }
        });
        
      }
      
    } else {
      clearInterval(_this.timeoutId)
    }

  },
  formSubmit: function (e) {
    const params = e.detail.value

    if (!Validate.checkForm(e)) {

      const error = Validate.errorList[0]
      app.showToast(error.msg, false, 500, '../../assets/warning.png')
      //提示信息  
      // console.log(error, e)
      return false
    }

    app.query('/api/users/reset_password', e.detail.value, 'post').then(res => {
      // console.log(res)
      if (res.status == 'success') {
        app.showToast('密码重置成功', 'success', 500)
        // wx.reLaunch({
        //   url: '../logs/logs',
        // })
        
        setTimeout(()=>{
          wx.navigateBack({
            delta: 1
          })
        },600)
        

      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(this.timeoutId);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})